package com.triptogether.receivers;

import android.content.Context;
import android.content.Intent;

import com.triptogether.utils.db.DBManager;
import com.triptogether.utils.db.models.Message;

public class MessageCreatedReceiver extends DBObjectCreatedReceiver {
	
	public static final String INTENT_FILTER_ACTION_MESSAGE_CREATED = DBManager.class.getName() + "message.created";
	public static final String INTENT_MESSAGE_CREATE_KEY_MESSAGE_ID = "messageId";

	public MessageCreatedReceiver(DBObjectCreatedListener listener) {
		registerToDbObejctCreatedReceiverListener(listener);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// Get the message id from the intent
		long messageId = intent.getLongExtra(MessageCreatedReceiver.INTENT_MESSAGE_CREATE_KEY_MESSAGE_ID, Long.MIN_VALUE);
		
		if (messageId != Long.MIN_VALUE) {
			// If the intent was sent with the extra get the message from
			// the local db and add it to the list
			Message newMessage = DBManager.getInstance(context).findMessageById(messageId);
			onDbObjectCreated(newMessage);
		}
	}
}
